package org.jeecg.modules.system.service.impl;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.jeecg.modules.system.bo.QrCodeBO;
import org.jeecg.modules.system.service.IQrCodeService;

import javax.servlet.ServletOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: xiamu
 * @version: 1.0
 * @date: 2021/7/23 18:01
 */
public class QrCodeServiceImpl implements IQrCodeService {

    /**
     * 生成二维码
     * @param qrCodeBO
     * @return
     */
    @Override
    public BitMatrix create(QrCodeBO qrCodeBO) throws WriterException {
        //todo 校验参数是否合法
        int width = 200;
        int height = 200;
        String format = "png";
        //todo   生成盐

        //todo 使用 二维码类型 +uuid（MD5 +salt）
        String content = "12345678";
        Map<EncodeHintType,Object> config = new HashMap<>(3);
        config.put(EncodeHintType.CHARACTER_SET,"UTF-8");
        config.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        config.put(EncodeHintType.MARGIN, 0);

        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,config);

        return bitMatrix;
    }
}
